package com.combetter.changxing.viewmodel;

import android.app.Application;
import android.bluetooth.BluetoothGatt;
import android.content.Intent;
import android.databinding.ObservableField;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.combetter.changxing.MyApp;
import com.combetter.changxing.R;
import com.combetter.changxing.bluetooth.FacilityClient2;
import com.combetter.changxing.db.entity.User;
import com.combetter.changxing.net.service.APIService;
import com.combetter.changxing.ui.activity.MainActivity;
import com.combetter.changxing.ui.activity.MessageActivity;
import com.combetter.changxing.ui.activity.MineCollectActivity;
import com.combetter.changxing.ui.activity.MineRecordActivity;
import com.combetter.changxing.ui.activity.MineRegardActivity;
import com.combetter.changxing.ui.activity.MineUpgradeActivity;
import com.combetter.changxing.ui.activity.MineUserInfoActivity;
import com.combetter.changxing.ui.dialog.BelowSelectDialog;
import com.combetter.changxing.ui.dialog.ExitDialog;
import com.combetter.changxing.ui.dialog.MerchantDialog;
import com.combetter.changxing.util.FragmentUtils;
import com.combetter.changxing.util.LogUtil;
import com.combetter.changxing.util.ToastUtils;
import com.combetter.changxing.viewmodel.base.BaseViewModel;

import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.combetter.changxing.constanst.Constants.REUEST_CODDE;

/**
 * Created by 深圳唐龙飞讯科技有限公司 on 2018/6/7.
 */

public class MineViewModel extends BaseViewModel {
    //温度微调Id
    public static int temperatureId = 1;
    //加热时间微调Id
    public static int warmId = 2;
    //预热时间微调Id
    public static int preheatId = 3;

    //温度微调
    public ObservableField<String> temperature = new ObservableField<>("0℃");
    //加热时间微调
    public ObservableField<String> warm = new ObservableField<>("15");
    //预热时间微调
    public ObservableField<String> preheat = new ObservableField<>("0");

    public MineViewModel(@NonNull Application application) {
        super(application);
    }


    /**
     * 退出
     *
     * @param view
     */
    public void exit(View view) {
        new ExitDialog().show(((MainActivity) view.getContext()).getSupportFragmentManager(), "");
    }

    /**
     * 点击事件
     */
    public void onClick(View view) {
        switch (view.getId()) {
            /**
             * 修改资料
             */
            case R.id.info_layout:
                view.getContext().startActivity(new Intent(view.getContext(), MineUserInfoActivity.class));
                break;
            /**
             * 固件升级
             */
            case R.id.rl_upgrade:
                if (FacilityClient2.mConnectState == BluetoothGatt.STATE_CONNECTED) {
                    if (MyApp.getInstance().getUserInfo() != null && !TextUtils.isEmpty(MyApp.getInstance().getUserInfo().getFacilityVersion())) {
                        view.getContext().startActivity(new Intent(view.getContext(), MineUpgradeActivity.class).putExtra("version", MyApp.getInstance().getUserInfo().getFacilityVersion()));
                    }
                } else {
                    ToastUtils.showToastShort("您未连接设备，请连接设备");
                }
                break;

            /**
             * 我的收藏
             */
            case R.id.collect_layout:
                view.getContext().startActivity(new Intent(view.getContext(), MineCollectActivity.class));
                break;

            /**
             * 吸烟记录
             */
            case R.id.record_layout:
                view.getContext().startActivity(new Intent(view.getContext(), MineRecordActivity.class));
                break;

            /**
             * 关于我们
             */
            case R.id.regard_layout:
                view.getContext().startActivity(new Intent(view.getContext(), MineRegardActivity.class));
                break;
            /**
             * 关于我们
             */
            case R.id.message_layout:
                view.getContext().startActivity(new Intent(view.getContext(), MessageActivity.class));
                break;

            /**
             * 联系商家
             */
            case R.id.merchant_layout:
                MerchantDialog dialog = new MerchantDialog();
                dialog.show(((MainActivity) view.getContext()).getSupportFragmentManager(), "");
                break;


        }
    }


    /**
     * 温度微调
     *
     * @param view
     */
    public void temperatureTrimming(View view) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = -10; i < 11; i++) {
            if (i % 2 == 0) {
                list.add(i + "℃");
            }
        }
        BelowSelectDialog dialog = new BelowSelectDialog();
        Bundle bundle = new Bundle();
        bundle.putStringArrayList("mOptionsItems", list);
        bundle.putInt("titleId", temperatureId);
        dialog.setArguments(bundle);
        Fragment fragment = FragmentUtils.getTopShowFragment(((MainActivity) view.getContext()).getSupportFragmentManager());
        dialog.setTargetFragment(fragment, REUEST_CODDE);
        dialog.show(((MainActivity) view.getContext()).getSupportFragmentManager(), "");
    }

    /**
     * 加热时间微调
     *
     * @param view
     */
    public void warmTrimming(View view) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = -4; i < 13; i++) {
            if (i % 2 == 0) {
                list.add(i + "");
            }
        }
        BelowSelectDialog dialog = new BelowSelectDialog();
        Bundle bundle = new Bundle();
        bundle.putStringArrayList("mOptionsItems", list);
        bundle.putInt("titleId", warmId);
        dialog.setArguments(bundle);
        Fragment fragment = FragmentUtils.getTopShowFragment(((MainActivity) view.getContext()).getSupportFragmentManager());
        dialog.setTargetFragment(fragment, REUEST_CODDE);
        dialog.show(((MainActivity) view.getContext()).getSupportFragmentManager(), "");
    }

    /**
     * 预热时间微调
     *
     * @param view
     */
    public void preheatTrimming(View view) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = -4; i < 13; i++) {
            if (i % 2 == 0) {
                list.add(i + "");
            }
        }
        BelowSelectDialog dialog = new BelowSelectDialog();
        Bundle bundle = new Bundle();
        bundle.putStringArrayList("mOptionsItems", list);
        bundle.putInt("titleId", preheatId);
        dialog.setArguments(bundle);
        Fragment fragment = FragmentUtils.getTopShowFragment(((MainActivity) view.getContext()).getSupportFragmentManager());
        dialog.setTargetFragment(fragment, REUEST_CODDE);
        dialog.show(((MainActivity) view.getContext()).getSupportFragmentManager(), "");
    }

    /**
     * 温度微调
     */
    public void setTemperature(String value0) {
        int value = Integer.parseInt(value0.replace("℃", ""));
        LogUtil.e("----------" + (byte) value);
        byte[] data = new byte[]{(byte) value};
        FacilityClient2.getClient().sendFever(data);
    }

    /**
     * 加热时间微调
     */
    public void setWarm() {
//        int value = Integer.parseInt(warm.get());
//        byte[] data = new byte[]{(byte) value};
//        FacilityClient.getClient().sendWarm(data);
    }

    /**
     * 预热时间微调
     */
    public void setPreheat(String value0) {
        int value = Integer.parseInt(value0.replace("s", ""));
        byte[] data = new byte[]{(byte) value};
        FacilityClient2.getClient().sendPreheat(data);
    }

    /**
     * 获取用户信息
     */
    public void getUserInfo() {
        String uid = MyApp.getInstance().getUserInfo().getUid();
        mHttpRequest.createService(APIService.class)
                .getUserInfo(uid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .doOnError(throwable -> LogUtil.e("获取用户信息异常：" + throwable.getMessage()))
                .subscribe(userHttpResult -> {
                    LogUtil.e("获取用户信息成功：" + new Gson().toJson(userHttpResult));
                    if (userHttpResult.getCode() == 200) {
                        User user = userHttpResult.getData();
                        user.setPhone(user.getPhone());
                        MyApp.getInstance().getDatabase().userDao().insertUser(user);

                    } else {
                        ToastUtils.error(userHttpResult.getMsg());
                    }

                });
    }


}
